package wc;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import wc.controller.buttonlisteners.ButtonBenchListener;
import wc.controller.buttonlisteners.ComputerStoreButtonListener;
import wc.controller.buttonlisteners.ShoeStoreButtonListener;
import wc.model.Mall;
import wc.model.SimpleFloor;
import wc.model.component.Component;
import wc.model.component.MallComponent;
import wc.model.component.MallRoom;
import wc.model.component.roomcomponent.RoomComponent;
import wc.model.grid.TileGrid;
import wc.model.screen.Art;

public class MallApplication extends Canvas implements MouseListener, Observer {
	private static final long serialVersionUID = 1L;
	
	public static int GAME_WIDTH = 320;
	public static int GAME_HEIGHT = 320;

	public static final int TILE_WIDTH = 32;
	public static final int TILE_HEIGHT = 32;
	
	private Mall mall;
	public static Component selectedComponent = null;
	public static java.util.List<RoomComponent> needToPlaceComponents = new ArrayList<RoomComponent>();
	
	public MallApplication() {
		mall = new Mall();
		Art.loadResources(mall.getScreen());
		
		mall.addFloor(new SimpleFloor(new TileGrid<MallComponent>(10, 10)));
		mall.addFloor(new SimpleFloor(new TileGrid<MallComponent>(30, 1)));
		
		setMinimumSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
		setMaximumSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
		setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
		
		addMouseListener(this);
		mall.addObserver(this);
		initializeScreen();
		
		mall.render();
	}
	
	public void initializeScreen() {
		System.out.println("Starting application");
		
		JFrame frame = new JFrame("Mall Application Thingie");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel mainPanel = new JPanel(new BorderLayout());
		mainPanel.setMinimumSize(new Dimension(420, 320));
		mainPanel.setMaximumSize(new Dimension(420, 320));
		mainPanel.setPreferredSize(new Dimension(420, 320));
		
		JPanel mallPanel = new JPanel();
		mallPanel.setMinimumSize(new Dimension(320, 320));
		mallPanel.setMaximumSize(new Dimension(320, 320));
		mallPanel.setPreferredSize(new Dimension(320, 320));
		
		mallPanel.add(this);
		
		JPanel itemsPanel = new JPanel(new FlowLayout());

		itemsPanel.setMinimumSize(new Dimension(100, 320));
		itemsPanel.setMaximumSize(new Dimension(100, 320));
		itemsPanel.setPreferredSize(new Dimension(100, 320));

		JButton benchButton = new JButton("Bench");
		benchButton.setSize(new Dimension(15, 15));
		benchButton.addActionListener(new ButtonBenchListener());

		JButton shoeStorebutton = new JButton("Shoe Store");
		shoeStorebutton.setSize(new Dimension(15, 15));
		shoeStorebutton.addActionListener(new ShoeStoreButtonListener());

		JButton computerStorebutton = new JButton("Computer Store");
		computerStorebutton.setSize(new Dimension(15, 15));
		computerStorebutton.addActionListener(new ComputerStoreButtonListener());
		
		itemsPanel.add(benchButton);
		itemsPanel.add(shoeStorebutton);
		itemsPanel.add(computerStorebutton);
		
		itemsPanel.setBackground(java.awt.Color.CYAN);
		mainPanel.add(mallPanel, BorderLayout.WEST);
		mainPanel.add(itemsPanel, BorderLayout.EAST);
		
		
		frame.setContentPane(mainPanel);
		frame.pack();
		frame.setResizable(true);
		frame.setVisible(true);
	}

	private void doPaint(Graphics g) {
		BufferedImage image = mall.getScreen().getImage();
		g.drawImage(image, 0, 0, GAME_WIDTH, GAME_HEIGHT, null);
	}

	@Override
	public void paint(Graphics g) {
		doPaint(g);
		validate();
	}

	@Override
	public void update(Graphics g) {
		mall.render();
		doPaint(g);
	}

	@Override
	public void mouseClicked(MouseEvent event) {
		if(selectedComponent != null) {
			int x = (event.getX() / TILE_WIDTH);
			int y = (event.getY() / TILE_HEIGHT);
			if(needToPlaceComponents.size() > 0) { //You're building a room that has requirements?
				if(selectedComponent == needToPlaceComponents.get(0)) { //Room already placed?	
					Component c = mall.getFloor(mall.getFloorIndex()).getTileGrid().getTiles()[x][y];
					if(c instanceof MallRoom) {//Is this a room you're trying to place the component in?
						MallRoom m = (MallRoom) c;
						if(m.getMustPlaceComponents().size() > 0) { //Is this the room that needs the required component?
							if(mall.addComponent(x, y, needToPlaceComponents.get(0))) { //Placing it correctly inside the room?
								System.out.println("Placed: "+needToPlaceComponents.get(0).getName());
								needToPlaceComponents.remove(0);
								if(needToPlaceComponents.size() == 0) {
									selectedComponent = null;
									System.out.println("Done placing the "+m.getName());
								} else {
									selectedComponent = needToPlaceComponents.get(0);
									System.out.println("Place your "+selectedComponent.getName());
								}
							} else {
								System.out.println("Cannot place "+selectedComponent.getName()+" here");
							}
						} else {
							System.out.println("Need to place "+selectedComponent.getName()+" in the correct room");
						}
					} else {
						System.out.println("Need to place the "+selectedComponent.getName()+" in the room you just placed");
					}
				} else { //place the room first
					if(mall.addComponent(x, y, selectedComponent)) {
						System.out.println("Placed: "+selectedComponent.getName());
						selectedComponent = needToPlaceComponents.get(0);
						System.out.println("Place your "+selectedComponent.getName());
					} else {
						System.out.println("Cannot place "+selectedComponent.getName()+" here");
					}
				}
			} else { //not a room with requirements
				if(mall.addComponent(x, y, selectedComponent)) {
					System.out.println("Placed: "+selectedComponent.getName());
					selectedComponent = null;
				} else {
					System.out.println("Cannot place "+selectedComponent.getName()+" here");
				}
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent event) {
		// TODO Auto-generated method stub
		
	}
	
	public static void main(String[] args){
		new MallApplication();
	}

	@Override
	public void update(Observable o, Object arg) {
		repaint();
	}
}
